using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using InvoiceProject.Models;

namespace InvoiceProject.Controllers
{   
    public class InvoicesController : Controller
    {
        private InvoiceProjectContext context = new InvoiceProjectContext();

        //
        // GET: /Invoices/

        public ViewResult Index()
        {
            return View(context.Invoices.Include(invoice => invoice.InvoiceItems).ToList());
        }

        //
        // GET: /Invoices/Details/5

        public ViewResult Details(long id)
        {
            Invoice invoice = context.Invoices.Single(x => x.Id == id);
            return View(invoice);
        }

        //
        // GET: /Invoices/Create
        [Authorize]
        public ActionResult Create()
        {

            return View(new Invoice {InvoiceItems = new List<InvoiceItem>(), TotalIncTax = 43.32M});
        } 

        //
        // POST: /Invoices/Create

        [HttpPost]
        public ActionResult Create(Invoice invoice)
        {
            if (ModelState.IsValid)
            {
             
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            return View(invoice);
        }
        
        //
        // GET: /Invoices/Edit/5
 
        public ActionResult Edit(long id)
        {
            Invoice invoice = context.Invoices.Single(x => x.Id == id);
            return View(invoice);
        }

        //
        // POST: /Invoices/Edit/5

        [HttpPost]
        public ActionResult Edit(InvoiceCreateEditViewModel invoice)
        {
            if (ModelState.IsValid)
            {
                context.Entry(invoice).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            return View(invoice);
        }

        //
        // GET: /Invoices/Delete/5
 
        public ActionResult Delete(long id)
        {
            Invoice invoice = context.Invoices.Single(x => x.Id == id);
            return View(invoice);
        }

        //
        // POST: /Invoices/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(long id)
        {
            Invoice invoice = context.Invoices.Single(x => x.Id == id);
            context.Invoices.Remove(invoice);
            context.SaveChanges();
            return RedirectToAction("Index");
        }



        //
        // GET: /Account/Register

        [AllowAnonymous]
        public ActionResult AddInvoiceItem()
        {
            return ContextDependentView();
        }

        //
        // POST: /Account/JsonRegister

        [AllowAnonymous]
        [HttpPost]
        public ActionResult JsonRAddInvoiceItem(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                //MembershipCreateStatus createStatus;
                //Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

                //if (createStatus == MembershipCreateStatus.Success)
                //{
                //    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                   return Json(new { success = true });
                //}
                //else
                //{
                //    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                //}
            }

            // If we got this far, something failed
           // return Json(new { errors = GetErrorsFromModelState() });
            return Json(new { success = true });
        }

        //
        // POST: /Account/Register


        [HttpPost]
        public ActionResult AddInvoiceItem(InvoiceCreateEditViewModel model)
        {
           // model.Invoice.InvoiceItems.Add(model.NewInvoiceItem);
          //  Create(model);
            if (ModelState.IsValid)
            {
                //// Attempt to register the user
                //MembershipCreateStatus createStatus;
                //Membership.CreateUser(model.UserName, model.Password, model.Email, null, null, true, null, out createStatus);

                //if (createStatus == MembershipCreateStatus.Success)
                //{
                //    FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
                 
                //}
                //else
                //{
                //    ModelState.AddModelError("", ErrorCodeToString(createStatus));
                //}
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }



        private ActionResult ContextDependentView()
        {
            string actionName = ControllerContext.RouteData.GetRequiredString("action");
            if (Request.QueryString["content"] != null)
            {
                ViewBag.FormAction = "Json" + actionName;
                return PartialView();
            }
            else
            {
                ViewBag.FormAction = actionName;
                return View();
            }
        }






    }
}